package com.tgy.MyFlowable.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.core.common.enums.StatusType;
import com.ctsi.core.common.exception.MarsException;
import com.ctsi.core.common.util.$;
import com.ctsi.core.common.util.StringUtil;
import com.ctsi.core.database.entity.Search;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.workflow.dto.FlwFormResourceDTO;
import com.ctsi.workflow.entity.FlwFormResource;
import com.ctsi.workflow.enums.FlwFormResourceExceptionEnum;
import com.ctsi.workflow.mapper.FlwFormResourceMapper;
import com.ctsi.workflow.service.IFlwFormResourceService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 流程脚本表 服务实现类
 *
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/8 19:35
 */
@Service
public class FlwFormResourceServiceImpl extends ServiceImpl<FlwFormResourceMapper, FlwFormResource> implements IFlwFormResourceService {

    @Override
    public IPage<FlwFormResource> listPage(Search search) {
        LambdaQueryWrapper<FlwFormResource> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtil.isNotBlank(search.getKeyword())) {
            queryWrapper.like(FlwFormResource::getId, search.getKeyword());
        }
        queryWrapper.orderByDesc(FlwFormResource::getCreateTime);
        return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
    }

    @Override
    public FlwFormResource detail(FlwFormResourceDTO flwFormResourceDTO) {
        return this.queryFlwFormResource(flwFormResourceDTO);
    }

    @Override
    public List<FlwFormResource> list(FlwFormResourceDTO flwFormResourceDTO) {
        boolean isName = $.isNotBlank(flwFormResourceDTO.getName());
        boolean isCode = $.isNotBlank(flwFormResourceDTO.getCode());
        boolean isCategory = $.isNotBlank(flwFormResourceDTO.getCategory());
        return this.list(Wrappers.<FlwFormResource>lambdaQuery()
                .like(isName, FlwFormResource::getName, flwFormResourceDTO.getName())
                .like(isCode, FlwFormResource::getCode, flwFormResourceDTO.getCode())
                .eq(isCategory, FlwFormResource::getCategory, flwFormResourceDTO.getCategory())
                .eq(FlwFormResource::getStatus, StatusType.ENABLE.getCode()));
    }

    private FlwFormResource queryFlwFormResource(FlwFormResourceDTO flwFormResourceDTO) {
        FlwFormResource flwFormResource = this.getById(flwFormResourceDTO.getId());
        if ($.isNull(flwFormResource)) {
            throw new MarsException(FlwFormResourceExceptionEnum.FORM_RESOURCE_NOT_EXIST);
        }
        return flwFormResource;
    }
}
